package kafka.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created by Administrator on 2016/12/2.
 */
public class JDBCUntil {

    public static Connection getConnection() throws SQLException, ClassNotFoundException {

        //声明Connection对象
        Connection con = null;
        //驱动程序名
        String driver = ResourceBundleUtils.getProperty("jdbc.driver");
        //URL指向要访问的数据库名mydata
        String url =  ResourceBundleUtils.getProperty("jdbc.url");
        //MySQL配置时的用户名
        String user =  ResourceBundleUtils.getProperty("jdbc.user");
        //MySQL配置时的密码
        String password =  ResourceBundleUtils.getProperty("jdbc.password");
        //遍历查询结果集
        try {
            //加载驱动程序
            Class.forName(driver);
            //1.getConnection()方法，连接MySQL数据库！！
            con = DriverManager.getConnection(url, user, password);
            return con;

        } catch (ClassNotFoundException e) {
            //数据库驱动类异常处理
            System.out.println("Sorry,can`t find the Driver!");
            throw  e;
        } catch (SQLException e) {
            //数据库连接失败异常处理
            throw e;

        }  finally {
            System.out.println("数据库数据成功获取！！");

            return con;
        }
    }

}
